
import { CACHE_KEY, useCache, deleteUserCache } from '@/hooks/web/useCache'

const isArray = function (obj: any): boolean {
  return Object.prototype.toString.call(obj) === '[object Array]'
}

const Comm = () => {}

const baseImgUrl = ref(import.meta.env.VITE_APP_DEFAULT_IMGURL)

Comm.getThisUser = function () {
  const { wsCache } = useCache();
  const LOGINUSERa = wsCache.get(CACHE_KEY.LOGINUSER);
  if (LOGINUSERa ) {
    const LOGINUSER =  JSON.parse(LOGINUSERa)
     return LOGINUSER;
  }
  return null;
}


Comm.saveImg = function (url : any) {
 
  if(Array.isArray(url)){
    if(url.length==0){
      return [];
    }
    for (let j=0;j<  url.length;j++) {
      url[j]=(url[j].replace(baseImgUrl.value,"").replace("blob:",""))
    }
    return url;
  }else{
       if(!url||url==null||url.trim()==''){
        return url;
       }
       const uisd=url.split(",");
       for (let j=0;j<  uisd.length;j++) {
        uisd[j]=(uisd[j].replace(baseImgUrl.value,"").replace("blob:",""))
      }
      return uisd.join(",");
  }
  return url?  url.split(baseImgUrl.value).join(",").split(",,").join(","):url;
}
Comm.getImgOne = function (url : any) {
  if(url){
    if(url.indexOf("http")==-1 &&  url.indexOf(baseImgUrl.value)==-1){
      url= baseImgUrl.value+url; 
    }
  }
  return url;
}

Comm.getImg = function (url : any) {
  
   if(!url){return}
  if(Array.isArray(url)){
    for (let j=0;j<url.length;j++) {
      if(url[j].indexOf(baseImgUrl.value)==-1){
      
        url[j]=(baseImgUrl.value+url[j]);
      }else{
        url[j]= url[j];
      }
    }
    return url;

  }else{
    const hiu=url?url.split(","):[];
    const iou= [] as any ;
    for (const key in hiu) {
      if(hiu[key].indexOf(baseImgUrl.value)==-1){
       
        iou.push(baseImgUrl.value+hiu[key])
      }else{
        iou.push(hiu[key])
      }
    }
   
    return iou.join(",");
  }
  
}

export default Comm
